var ss = ["待付款","待发货","待收货","签收完成","拒收退款"];
var statusFormatter = function(v){
	return `<b class="text-danger">${ss[v]}</b>`;
}

var indexFormatter = function(value, row, index) {
	return index + 1;
}

var moneyFormatter = function(v){
	try{
		return `<b class="text-danger">￥${v.toFixed(2)}</b>`;
	}catch(e){
		return "-";
	}
}

var productFormatter = function(v,r,i) {
	let productImgs = JSON.parse(r.productImgs);
	if(productImgs.length>0){
		productImgs = productImgs[0];
	}
	return `<a href="mall-product.let?productId=${v}" target="_blank"><img class="img-thumbnail" style="width:100px;" onerror="this.src='assets/img/no-img.png'" src="${r.productImgs}">${r.productName}`;
}

var id = new URLSearchParams(location.search).get("id");

var datagrid = $("._datagrid");//分页表格


/**
	初始化数据表格w
 */
var initTable = ()=>{
	datagrid.bootstrapTable({
		url: "user/order.let?action=page&orderId="+id,
		dataField: "list",//rows
		totalField: "total",
		queryParamsType: "",//limit
		pagination: true,
		sidePagination: "server"
	});
}


initTable();


var loadData = ()=>{
	let info = $("._info");
	console.info("loadDate");
	return new Promise((s,e)=>{
		$.get(
			"user/order.let?action=get",
			{"id":id},
			function(data){
				data = data.data;
				for(let name in data){
					let value = data[name];
					console.info(`${name} = ${value}`);
					info.find("[data-field='"+name+"']").each(function(index,ele){
						console.info(ele);
						let fn = $(ele).data("formatter");
						if(fn=="" || fn == null){
							$(ele).text(value);
						}else{
							try{
								fn = eval(fn);
								$(ele).html(fn(value,data,0));
							}catch(e){ v   
								console.error(e);
								$(ele).text(value);
							}
						}
						
					});
				}
				data = data.order;
				for(let name in data){
					let value = data[name];
					console.info(`${name} = ${value}`);
					info.find("[data-field='"+name+"']").each(function(index,ele){
						console.info(ele);
						let fn = $(ele).data("formatter");
						if(fn=="" || fn == null){
							$(ele).text(value);
						}else{
							try{
								fn = eval(fn);
								$(ele).html(fn(value,data,0));
							}catch(e){ v   
								console.error(e);
								$(ele).text(value);
							}
						}
						
					});
				}
				s();
			},"json"
		);
		
	});
}
loadData();
